import pymysql
from common.handle_conf import conf
from contextlib import closing


class DB:
    def __init__(self, ):
        self.connect = pymysql.connect(host=conf.get('mysql', 'host'),
                                       port=conf.getint('mysql', 'port'),
                                       user=conf.get('mysql', 'user'),
                                       password=conf.get('mysql', 'password'),
                                       charset='utf8',
                                       # cursorclass=pymysql.cursors.DictCursor
                                       )

    # 返回查询到的数量
    def find_count(self, sql):
        with closing(self.connect.cursor()) as cur:
            res = cur.execute(sql)
            return res

    # 返回查询到的所有数据
    def find_all(self, sql):
        with closing(self.connect.cursor()) as cur:
            cur.execute(sql)
            res = cur.fetchall()
            return res

    # 返回查询到的第一条数据
    def find_one(self, sql):
        with closing(self.connect.cursor()) as cur:
            cur.execute(sql)
            res = cur.fetchone()
            return res


if __name__ == '__main__':
    db =DB()
    sql = 'SELECT * FROM futureloan.member WHERE mobile_phone=15518823479'
    res = db.find_one(sql)
    print(res)